<?php
class ContentFetcher extends Extractor
{
  
  const DEEP_LEVEL = 3;  
  
  private $stream;

  private $stringtofind;
  
  
  public function findOnPageAndPutResultsToFile($stringtofind, $url, $folder)
  {
    $this->stringtofind = $stringtofind;
    
    $this->stream = fopen($folder . "/" . $this->getDomain($url), "wb+");
    
    $this->doParsePage($url, - 1);
  }
  
  
  public function doParsePage($url, $level)
  {
    $level ++;
    $this->count_page ++;
    $this->processed[$url] = $level;
    $links = $this->getInnerLinks($url);
    
    foreach($links as $link)
      if(! array_key_exists($link, $this->processed))
        $this->processed[$link] = $level + 1;
        
    if($level < self::DEEP_LEVEL)
    {
      foreach($links as $link)
      {
        if(isset($this->processed[$link]) && is_numeric($this->processed[$link]))
        {
          if($this->processed[$link] == $level + 1)
          {
            $this->doParsePage($link, $level);
            
            $this->processed[$link] = null;
          }
        }
        continue;
      }
    }
    
    echo "Level: ", $level, " Link: ", $url, "\n";
    
    $status = "Not found";
    
    $position = mb_strpos($this->content_raw, $this->stringtofind);
    
    if($position!==false)
    {
      $status = "Found, position: ".$position;
    }
    
    fwrite($this->stream, "Page: ".$url.", Needs to find: ".$this->stringtofind.", Status: ".$status.";\n");
  }  
  
}
?>